package com.dianping.cat.home.service.transform;
import com.dianping.cat.home.service.entity.Domain;
import com.dianping.cat.home.service.entity.ServiceReport;
import java.io.ByteArrayInputStream;
import java.io.DataInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Date;
import com.dianping.cat.home.service.IVisitor;

public class DefaultNativeParser implements IVisitor {
  private DefaultLinker m_linker;

  public DefaultNativeParser(InputStream in) {
/*  14 */     this.m_linker = new DefaultLinker(true);




/*  19 */     this.m_in = new DataInputStream(in);
  }
  private DataInputStream m_in;

/*  23 */   public static ServiceReport parse(byte[] data) { return parse(new ByteArrayInputStream(data)); }


  public static ServiceReport parse(InputStream in) {
/*  27 */     DefaultNativeParser parser = new DefaultNativeParser(in);
/*  28 */     ServiceReport serviceReport = new ServiceReport();

    try {
      serviceReport.accept(parser);
    } catch (RuntimeException e) {
/*  33 */       if (e.getCause() == null || !(e.getCause() instanceof java.io.EOFException))
      {

        throw e;
      }
    }

/*  40 */     parser.m_linker.finish();
/*  41 */     return serviceReport;
  }



  public void visitDomain(Domain domain) {
    byte tag;
/*  48 */     while ((tag = readTag()) != -1) {
/*  49 */       visitDomainChildren(domain, (tag & 0xFF) >> 2, tag & 0x3);
    }
  }

  protected void visitDomainChildren(Domain domain, int _field, int _type) {
/*  54 */     switch (_field) {
      case 1:
/*  56 */         domain.setId(readString());
        break;
      case 2:
/*  59 */         domain.setTotalCount(readLong());
        break;
      case 3:
/*  62 */         domain.setFailureCount(readLong());
        break;
      case 4:
/*  65 */         domain.setFailurePercent(readDouble());
        break;
      case 5:
/*  68 */         domain.setSum(readDouble());
        break;
      case 6:
/*  71 */         domain.setAvg(readDouble());
        break;
      case 7:
/*  74 */         domain.setQps(readDouble());
        break;
    }
  }



  public void visitServiceReport(ServiceReport serviceReport) {
    byte tag;
/*  83 */     if ((tag = readTag()) != -4) {
/*  84 */       throw new RuntimeException(String.format("Malformed payload, expected: %s, but was: %s!", new Object[] { Integer.valueOf(-4), Byte.valueOf(tag) }));
    }

/*  87 */     while ((tag = readTag()) != -1) {
/*  88 */       visitServiceReportChildren(serviceReport, (tag & 0xFF) >> 2, tag & 0x3);
    }
  }

  protected void visitServiceReportChildren(ServiceReport serviceReport, int _field, int _type) {
/*  93 */     switch (_field) {
      case 1:
        serviceReport.setStartTime(readDate());
        break;
      case 2:
/*  98 */         serviceReport.setDomain(readString());
        break;
      case 3:
/* 101 */         serviceReport.setEndTime(readDate());
        break;
      case 33:
/* 104 */         if (_type == 1) {
/* 105 */           Domain domain = new Domain();

/* 107 */           visitDomain(domain);
/* 108 */           this.m_linker.onDomain(serviceReport, domain); break;
/* 109 */         }  if (_type == 2) {
/* 110 */           for (int i = readInt(); i > 0; i--) {
/* 111 */             Domain domain = new Domain();

/* 113 */             visitDomain(domain);
/* 114 */             this.m_linker.onDomain(serviceReport, domain);
          }
        }
        break;
    }
  }

  private Date readDate() {
    try {
/* 123 */       return new Date(readVarint(64));
/* 124 */     } catch (IOException e) {
/* 125 */       throw new RuntimeException(e);
    }
  }

  private double readDouble() {
    try {
/* 131 */       return Double.longBitsToDouble(readVarint(64));
/* 132 */     } catch (IOException e) {
/* 133 */       throw new RuntimeException(e);
    }
  }

  private int readInt() {
    try {
/* 139 */       return (int)readVarint(32);
/* 140 */     } catch (IOException e) {
/* 141 */       throw new RuntimeException(e);
    }
  }

  private long readLong() {
    try {
/* 147 */       return readVarint(64);
/* 148 */     } catch (IOException e) {
/* 149 */       throw new RuntimeException(e);
    }
  }

  private String readString() {
    try {
/* 155 */       return this.m_in.readUTF();
/* 156 */     } catch (IOException e) {
/* 157 */       throw new RuntimeException(e);
    }
  }

  private byte readTag() {
    try {
/* 163 */       return this.m_in.readByte();
/* 164 */     } catch (IOException e) {
/* 165 */       throw new RuntimeException(e);
    }
  }

  protected long readVarint(int length) throws IOException {
/* 170 */     int shift = 0;
/* 171 */     long result = 0L;

/* 173 */     while (shift < length) {
/* 174 */       byte b = this.m_in.readByte();
/* 175 */       result |= (b & 0x7F) << shift;
/* 176 */       if ((b & 0x80) == 0) {
/* 177 */         return result;
      }
/* 179 */       shift += 7;
    }

/* 182 */     throw new RuntimeException("Malformed variable int " + length + "!");
  }
}


/* Location:              /Users/hanfei/Documents/apache-tomcat-9.0.24/webapps/cat/WEB-INF/classes/!/com/dianping/cat/home/service/transform/DefaultNativeParser.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       1.0.7
 */